﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sudoku.Lang;
using System.Diagnostics;
using Services;
using Sudoku.Menu;

namespace Sudoku.Menu
{
	public class HowtoScene : ExtendedGameComponent
	{
		private int currentScreen = 0;
		private List<String> currentInstructions;

		private LabelButton nextButton;
		private LabelButton prevButton;
		private LabelButton menuButton;

		private TransitionScreen transition;

		public HowtoScene(Game game)
			: base (game)
		{

		}

		public override void Initialize()
		{
			
			// Prepare instructions - move this routine somewhere else?
			List<String> guide = new List<string>();
			guide.Add("HOWTO_01");
			guide.Add("HOWTO_02");
			guide.Add("HOWTO_03");
			guide.Add("HOWTO_04");

			currentInstructions = guide;

			SpriteFont font = fontService.Font(FontType.Delicious26Bold);

			int dx = 7;

			// Next and prev button
			prevButton = new LabelButton(Game, new Rectangle(dx, 700, 150, 85), LocalizedStrings.HowToBack, font, Color.White);
			prevButton.Texture = textureService.Texture("Button1");
			prevButton.OnTap += new Button.ButtonEvent(OnPrevButtonRelease);
			this.Components.Add(prevButton);

			menuButton = new LabelButton(Game, new Rectangle(dx + 150 + dx, 700, 150, 85), "MENU", font, Color.White);
			menuButton.Texture = textureService.Texture("Button1");
			menuButton.OnTap += new Button.ButtonEvent(OnMenuButtonRelease);
			this.Components.Add(menuButton);

			nextButton = new LabelButton(Game, new Rectangle(dx + 150 + dx + 150 + dx, 700, 150, 85), LocalizedStrings.HowToNext, font, Color.White);
			nextButton.Texture = textureService.Texture("Button1");
			nextButton.OnTap += new Button.ButtonEvent(OnNextButtonRelease);
			this.Components.Add(nextButton);

			UpdateButtonStatus();

			transition = null;

			var inputService = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
			inputService.OnBack += new InputManager.PhoneButtonEvent(OnBackButton);

			base.Initialize();
			soundService.TurnPageSound();
		}

		protected override void Dispose(bool disposing)
		{
			nextButton.OnTap -= new Button.ButtonEvent(OnNextButtonRelease);
			menuButton.OnTap -= new Button.ButtonEvent(OnMenuButtonRelease);
			prevButton.OnTap -= new Button.ButtonEvent(OnPrevButtonRelease);

			base.Dispose(disposing);
		}

		private void TransitionEnd()
		{
			transition.OnTransitionEnd -= new Action(TransitionEnd);
			Game.Components.Remove(transition);
			transition.Dispose();
			transition = null;
		}

		private void StartTransition()
		{
			transition = new TransitionScreen(Game);
			transition.OnTransitionEnd += new Action(TransitionEnd);
			Game.Components.Add(transition);
		}

		private void OnBackButton()
		{
			if (currentScreen > 0)
			{
				OnPrevButtonRelease(null);
			}
			else
			{
				soundService.PlaySound(SoundType.Fumble);
				StateManagement.StateManager.StartMenu(Game);
			}
		}

		private void OnNextButtonRelease(Button sender)
		{
			StartTransition();
			++currentScreen;
			soundService.TurnPageSound();
		}

		private void OnPrevButtonRelease(Button sender)
		{
			StartTransition(); 
			--currentScreen;
			soundService.TurnPageSound();
		}

		private void OnMenuButtonRelease(Button sender)
		{
			soundService.PlaySound(SoundType.Fumble);
			StateManagement.StateManager.StartMenu(Game);	
		}

		private void UpdateButtonStatus()
		{
			prevButton.Enabled = currentScreen > 0;
			nextButton.Enabled = currentScreen < (currentInstructions.Count - 1);
		}

		public override void Update(GameTime gameTime)
		{
			UpdateButtonStatus();
			base.Update(gameTime);
		}

		public override void Draw(GameTime gameTime)
		{
			Texture2D currentTexture = textureService.Howto(currentInstructions[currentScreen]) as Texture2D;

			spriteBatch.Begin();
			spriteBatch.Draw(currentTexture, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
			spriteBatch.End();

			base.Draw(gameTime);
		}

	}
}
